/**
 * @Date: Mar 3, 2010 11:57:33 AM
 */
package com.philip.journal.home.dao;

import java.util.List;
import java.util.Map;

import com.philip.journal.core.dao.BaseDAOImpl;
import com.philip.journal.core.exception.JournalException;
import com.philip.journal.home.bean.Branch;
import com.philip.journal.home.bean.Entry;

/**
 * @author cry30
 */
public class EntryDAOImpl extends BaseDAOImpl<Entry> implements EntryDAO {

    @Override
    public Entry read(final long entryId) {
        try {
            return super.read(entryId);
        } catch (final JournalException e) {
            getLogger().debug(e.getMessage(), e);
            return super.readObject(Entry.ID, entryId);
        }
    }

    @Override
    public List<Entry> readAllByBranch(final long branchId) {
        return super.readAllByParent(Entry.PARENT, Branch.ATTR_BRANCH_ID, Long.valueOf(branchId));
    }

    @Override
    public void delete(final long entryId)
    {
        final Entry entry = super.readObject(Entry.ID, entryId);
        if (entry == null) {
            throw new JournalException("Delete failed for entryId [" + entryId + "]", "The object with the entryId ["
                    + entryId + "] was not found in the datastore.", "Cannot find [" + entryId + "]");
        }
        super.delete(entry);
    }

    @Override
    public void deleteAll(final List<Entry> entries)
    {
        super.deleteAll0(entries);
    }

    @Override
    public Entry readByTitle(final String title, final long branchId) {
        return super
                .readObjectByParent(Entry.PARENT, Branch.ATTR_BRANCH_ID, Long.valueOf(branchId), Entry.TITLE, title);
    }

    @Override
    public List<Entry> searchIlike(final Map<String, String> criteria) {
        return super.readAllIlike(criteria);
    }
}
